package earogya.pharmacy.request_for_drugs.messages;

import java.io.IOException;
import java.sql.Time;
import java.util.Calendar;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.datatype.EI;
import ca.uhn.hl7v2.model.v251.datatype.*;
import ca.uhn.hl7v2.model.v251.message.ORM_O01;
import ca.uhn.hl7v2.model.v251.segment.MSH;
import ca.uhn.hl7v2.model.v251.segment.ORC;
import ca.uhn.hl7v2.model.v251.segment.RXO;
import earogya.pharmacy.request_for_drugs.RequestForDrugsHibernateManager;
import earogya.pharmacy.request_for_drugs.Request_For_Drugs;

/*ORM_O01 (common order) message Handler
 * 
 * handles the common orders 
 * 
 * */
public class ORM_O01_Handler {

	private ORM_O01 adrMsg;
	private RequestForDrugsHibernateManager hibernate_manager = new RequestForDrugsHibernateManager();
	private Request_For_Drugs request = new Request_For_Drugs();

	public ORM_O01_Handler(ORM_O01 adrMsg) {
		this.adrMsg = adrMsg;
	}

	public String manageMessage() throws HL7Exception, IOException {
		String ack = null;
		try {
			ack = adrMsg.generateACK().encode();
			this.updateDrugRequestDetails();
		} catch (HL7Exception e) {
			ack = adrMsg.generateACK("OE", e).encode();
		}
		return ack;
	}

	public void updateDrugRequestDetails() throws NumberFormatException,
			HL7Exception {
		
		RXO rxo = adrMsg.getORDER().getORDER_DETAIL().getRXO();
		ORC orc = adrMsg.getORDER().getORC();
				
		EI rId = orc.getOrc2_PlacerOrderNumber();
		request.setRequestId(Integer.parseInt(rId.getEi1_EntityIdentifier()
				.getValue()));

		if (orc.getOrc7_QuantityTiming(0).getQuantity().getCq1_Quantity()
				.getValue() == null) {
		} else {
			request.setQuantity(Integer.parseInt(orc.getOrc7_QuantityTiming(0)
					.getQuantity().getCq1_Quantity().getValue()));
		}
		if (orc.getDateTimeOfTransaction().getTime().getValueAsDate() == null) {
		} else {
			request.setRequestDate(orc.getDateTimeOfTransaction().getTime()
					.getValueAsDate());
			request.setRequestTime(this.toTime());
		}
		if (orc.getOrc10_EnteredBy(0).getIDNumber().getValue() == null) {
		} else {
			request.setEnteredByID(Integer.parseInt(orc.getOrc10_EnteredBy(0)
					.getIDNumber().getValue()));
		}
		if (orc.getOrc11_VerifiedBy(0).getIDNumber().getValue() == null) {
		} else {
			request.setVerifiedByID(Integer.parseInt(orc.getOrc11_VerifiedBy(0)
					.getIDNumber().getValue()));
		}

		if (rxo.getRxo1_RequestedGiveCode().getIdentifier().getValue() == null) {

		} else {
			request.setDrugId(Integer.parseInt(rxo.getRxo1_RequestedGiveCode().getIdentifier().getValue()));
			request.setUrgency(rxo.getRxo1_RequestedGiveCode().getCe2_Text().getValue());
		}
		if (rxo.getRxo3_RequestedGiveAmountMaximum().getValue() == null) {
		} else {
			request.setMaxAmount(Integer.parseInt(rxo
					.getRxo3_RequestedGiveAmountMaximum().getValue()));
		}

		if (rxo.getRxo2_RequestedGiveAmountMinimum().getValue() == null) {

		} else {

			request.setMinAmount(Integer.parseInt(rxo
					.getRxo2_RequestedGiveAmountMinimum().getValue()));

		}

		if (rxo.getRxo4_RequestedGiveUnits().getIdentifier().getValue() == null) {

		} else {
			request.setAmountUnits(rxo.getRxo4_RequestedGiveUnits()
					.getIdentifier().getValue());
		}

		if (rxo.getRxo8_DeliverToLocation().encode() == null) {
		} else {
			request.setUnitOrWard(rxo.getRxo8_DeliverToLocation().encode());
		}

		hibernate_manager.saveObject(request);
	}

	@SuppressWarnings("deprecation")
	public Time toTime() throws DataTypeException {
		int hh = 0, mm = 0, ss = 0;
		ORC orc = adrMsg.getORDER().getORC();
		try {
			hh = orc.getDateTimeOfTransaction().getTime().getHour();
			mm = orc.getDateTimeOfTransaction().getTime().getMinute();
			ss = orc.getDateTimeOfTransaction().getTime().getSecond();
		} catch (DataTypeException e) {
		}
		Time time = new Time(hh, mm, ss);
		return time;

	}
}
